package cloudPayAdmin.syncTread;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.env.Environment;

import com.project.m.api.common.util.SpringUtils;

import cloudPayAdmin.admin.dbapp.entity.hicatcard.BaseEntity;
import cloudPayAdmin.admin.dbapp.entity.hicatcard.card.TSystemPresentCard;
import cloudPayAdmin.admin.service.hicatcard.card.SystemPresentCardService;
import cloudPayAdmin.constant.Constant;
import cloudPayAdmin.util.AdminUserInfoUtil;
import cloudPayAdmin.util.HttpUtil;
import cloudPayAdmin.util.IdGenerator;
import cloudPayAdmin.util.ParamHelper;
import cloudPayAdmin.util.TimeUtil;
import cloudPayAdmin.util.WxApiUtil;

public class SystemPresentCardMakeTask implements Callable<Map<String,List<String>>> {
	
	public final int makeCount;
	public final long cardId;
	public final long batch;
	
	  public SystemPresentCardMakeTask( int makeCount,long cardId,long batch) {
          this.makeCount = makeCount;
          this.cardId = cardId;
          this.batch = batch;
      }

	@Override
	public Map<String,List<String>> call() throws Exception {
		
		
		Environment env = SpringUtils.getEnvironment();
		
		List<String> qrCodeFiles = new ArrayList<String>();
		List<String> zipEntryNames = new ArrayList<String>();
		List<TSystemPresentCard> systemPresentCardList = new ArrayList<TSystemPresentCard>();
			
		//每个线程处理50个
		for(int i=0;i<makeCount;i++) {
			TSystemPresentCard systemPresentCard = new TSystemPresentCard();
			systemPresentCard.setCardId(cardId);	
			systemPresentCard.setBatch(batch);				
			String cardNumber = IdGenerator.generateSystemCardId(IdGenerator.ORDERID_PREFIX_SYSTEM_CARD, "00", "0000");
			systemPresentCard.setCardNumber(cardNumber);
			systemPresentCard.setStatus(BaseEntity.STATUS_IN_USE);
			systemPresentCard.setCreator(AdminUserInfoUtil.getLoginAdminUserName());
			systemPresentCard.setCreateTime(TimeUtil.getCurrTime(TimeUtil.DATE_PATTERN_NOSEPARTOR));
			systemPresentCard.setUpdateTime(TimeUtil.getCurrTime(TimeUtil.DATE_PATTERN_NOSEPARTOR));
			
			String databasePath = env.getProperty("image.upload.database.suffix.path") + Constant.PRESENTCARDQRCODE_PIC_FILE_PATH + HttpUtil.HTTP_PATH_SEPERATOR + cardId + "_" + Math.abs(System.nanoTime()) + ParamHelper.buildRandom(5) + ".png";
			String destinationPath = env.getProperty("image.upload.base.path") + StringUtils.replaceEach(databasePath, new String[]{env.getProperty("image.upload.database.suffix.path"),HttpUtil.HTTP_PATH_SEPERATOR}, new String[]{"",File.separator});
			FileUtils.copyInputStreamToFile(WxApiUtil.getUnLimitQrCode(cardNumber, "pages/card-detail/index"), new File(destinationPath));
			
			systemPresentCard.setQrCodePath(databasePath);
			
			systemPresentCardList.add(systemPresentCard);
			qrCodeFiles.add(destinationPath);
			zipEntryNames.add(cardNumber + ".png");
		}
		
		SystemPresentCardService systemPresentCardService = SpringUtils.getBean(SystemPresentCardService.class);
		systemPresentCardService.saveList(systemPresentCardList);
		
		Map<String, List<String>> map = new HashMap<String ,List<String>>();
		
		map.put("qrCodeFiles", qrCodeFiles);
		map.put("zipEntryNames", zipEntryNames);
		
		return map;
	}

}
